/*
 * @Author: wangdazhuang
 * @Date: 2024-08-30 15:55:20
 * @LastEditTime: 2024-08-31 15:03:56
 * @LastEditors: wangdazhuang
 * @Description: 
 * @FilePath: /pornhub_app/lib/src/views/mine/frontpage/controller/mine_index_controller.dart
 */
import 'dart:async';
import 'package:pornhub_app/http/http_service.dart';
import 'package:pornhub_app/model/mine/daily_signin_model.dart';
import 'package:pornhub_app/routes/routes.dart';
import 'package:pornhub_app/utils/utils.dart';
import 'package:get/get.dart';
import '../../../../components/easy_toast.dart';
import '../../../../model/announcement/announcement.dart';
import '../../../../services/storage_service.dart';
import '../../../../services/user_service.dart';
import '../../../../utils/ad_jump.dart';

class MineIndexController extends GetxController {
  final service = Get.find<StorageService>();
  int count = 0;
  final us = Get.find<UserService>();
  String vipSrc = "assets/mine/img_vip_open.png";
  String vipTitle = "你还未开通会员";
  var gold = 0.0.obs;
  var signData = DailySignInRespModel();
  bool isCheckIn = false;
  int checkDayNo = 0; //已经签到的第几天
  bool loginOut = false; //是否退出
  var appUrl = ''.obs;

  final List<String> titles = [
    '我的发布',
    '我的收藏',
    '我的购买',
    '离线下载',
    '观看记录',
    '在线客服',
    '官方社群',
    '应用推荐'
  ];
  final List<String> images = [
    'assets/mine/img_mine_tab_minepost.png',
    'assets/mine/img_mine_tab_minecollection.png',
    'assets/mine/img_mine_tab_shop.png',
    'assets/mine/img_mine_tab_onlineoff.png',
    'assets/mine/img_mine_tab_watchhistory.png',
    'assets/mine/img_mine_tab_online_service.png',
    'assets/mine/img_mine_tab_groups.png',
    'assets/mine/img_mine_tab_apps.png'
  ];

  final List<String> titlesWeb = [
    '我的发布',
    '我的收藏',
    '我的购买',
    '观看记录',
    '在线客服',
    '官方社群',
    '应用推荐'
  ];
  final List<String> imagesWeb = [
    'assets/mine/img_mine_tab_minepost.png',
    'assets/mine/img_mine_tab_minecollection.png',
    'assets/mine/img_mine_tab_shop.png',
    'assets/mine/img_mine_tab_watchhistory.png',
    'assets/mine/img_mine_tab_online_service.png',
    'assets/mine/img_mine_tab_groups.png',
    'assets/mine/img_mine_tab_apps.png'
  ];

  final List<int> indexNum = [1, 2, 3, 4, 5, 6, 7, 8];
  final List<int> indexNumWeb = [1, 2, 3, 5, 6, 7, 8];

  get userInfo => us.user;

  Future getUserInfo() async {
    await us.updateAPIUserInfo();
    count = userInfo.freeWatches! - userInfo.watched! > 0
        ? userInfo.freeWatches! - userInfo.watched!
        : 0;
    vipSrc = us.isVIP
        ? "assets/mine/img_vip_upgrade.png"
        : "assets/mine/img_vip_open.png";
    vipTitle = userInfo.expiredVip!.isEmpty
        ? "你还未开通会员"
        : '${Utils.dateFmt(userInfo.expiredVip!, [
                "yyyy",
                "-",
                "mm",
                "-",
                "dd"
              ])}到期';
    loginOut = userInfo.loginStatus == 2 ? true : false;
    update();
  }

  get assets => us.assets;

  Future getAssetsInfo() async {
    await us.updateAPIAssetsInfo();
    gold.value = us.assets.gold!;
    update();
  }

  Future getDailySignIn() async {
    signData = await httpInstance.get(
        url: "checkin/taskList", complete: DailySignInRespModel.fromJson);
    if (signData.isNull) return;
    if (signData is DailySignInRespModel) {
      isCheckIn = signData.status == 1 ? true : false;
      checkDayNo = signData.dayNo!;
    }
    update();
  }

  Future<void> postCheckIn() async {
    await httpInstance.post(url: "checkin/today");
    getAssetsInfo();
  }

  Future<void> getAppUrl() async {
    final ann = await httpInstance.get<AnnouncementModel>(
        url: 'sys/ann', complete: AnnouncementModel.fromJson);
    if (ann is AnnouncementModel) {
      appUrl.value = ann.appCenterUrl!;
    }
  }

  ///回调通知刷新
  void controllRefreshPage() {
    getUserInfo();
    getAssetsInfo();
    // getDailySignIn();
  }

  @override
  void onInit() {
    super.onInit();
    // getUserInfo();
    // getAssetsInfo();
    // getDailySignIn();
    // Get.find<UserService>().addListener(() => {
    //       getDailySignIn(),
    //     });
    getAppUrl();
  }

  @override
  void onReady() {
    super.onReady();
    // if (GetPlatform.isWeb) return;
    // jumpAccount();
  }

  void jumpAccount() {
    if (service.isMineFirst == false) {
      service.saveMineFirst(true);
      Get.toNamed(Routes.accountcredentials);
    }
  }

  void onCLickNavigation(int index) {
    switch (index) {
      case 1: //我的发布
        Get.toNamed(Routes.my_publications);
        break;
      case 2: //我的收藏
        Get.toNamed(Routes.favorite);
        break;
      case 3: //我的购买
        Get.toNamed(Routes.minebuy);
        break;
      case 4: //离线下载
        Get.toNamed(Routes.download);
        break;
      case 5: //观看记录
        Get.toNamed(Routes.watch);
        break;
      case 6: //在线客服
        kOnLineService();
        break;
      case 7: //加群聊骚
        Get.toNamed(Routes.mine_fans_followers, parameters: {'title': "加群聊骚"});
        break;
      case 8: //应用推荐
        if (appUrl.value.isEmpty) {
          EasyToast.show("暂无应用推荐");
        } else {
          jumpExternalAddress(appUrl.value, null);
        }
        break;
    }
  }
}
